//
//  NSDate+YFExtension.m
//  CalendarDemo
//
//  Created by Winny Zhang on 2019/4/2.
//  Copyright © 2019 Winny Zhang. All rights reserved.
//

#import "NSDate+YFExtension.h"


@implementation NSDate (YFExtension)

/**
 获取指定时间的 NSDateComponents
 
 @param date 指定的date
 @return NSDateComponents
 */
+ (NSDateComponents *)dateComponent:(NSDate *)date {
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *component = [[NSDateComponents alloc] init];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    component = [calendar components:unit fromDate:date];
    
    return component;
}

/**
 指定日期年份

 @return NSIteger 类型年份
 */
- (NSInteger)currentYear {
    return [[NSDate dateComponent:self] year];
}

/**
指定日期月份

 @return NSIteger 类型月份
 */
- (NSInteger)currentMonth {
    return [[NSDate dateComponent:self] month];
}

/**
 指定日期几号

 @return NSIteger 类型几号
 */
- (NSInteger)currentDay {
    return [[NSDate dateComponent:self] day];
}

/**
 指定日期小时

 @return NSIteger 类型小时
 */
- (NSInteger)currentHour {
    return [[NSDate dateComponent:self] hour];
}

/**
 指定日期分钟

 @return NSIteger 类型分钟
 */
- (NSInteger)currentMinute {
    return [[NSDate dateComponent:self] minute];
}

/**
 指定日期秒钟

 @return NSIteger 类型秒钟
 */
- (NSInteger)currentSecond {
    return [[NSDate dateComponent:self] second];
}

/**
 判断date是否在指定的时间段内，无序
 
 @param firstDate 时间段的 date1
 @param lastDate 时间段的 date2
 @return 是否在时间段内
 */
- (BOOL)isBetweenDate:(NSDate*)firstDate andDate:(NSDate*)lastDate {
    
    NSComparisonResult result = [firstDate compare:lastDate];
    // 降序
    if (result == NSOrderedDescending) {
        // firstDate  is in the future
        if ([self compare:firstDate] == NSOrderedDescending)
            return NO;
        if ([self compare:lastDate] == NSOrderedAscending)
            return NO;
        return YES;
        
        // 升序
    } else if (result == NSOrderedAscending) {
        // firtDate is in the past
        if ([self compare:firstDate] == NSOrderedAscending)
            return NO;
        if ([self compare:lastDate] == NSOrderedDescending)
            return NO;
        return YES;
        
        // 相等
    } else {
        // firtDate is in the past
        if ([self compare:firstDate] == NSOrderedDescending)
            return NO;
        if ([self compare:lastDate] == NSOrderedAscending)
            return NO;
        return YES;
    }
}

/**
 转换为当地时间的date

 @return 当d地时间的 date
 */
- (NSDate *)localeDate {
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: self];
    
    return [self dateByAddingTimeInterval: interval];
}



/**
 指定年月 返回当月天数

 @param year 年
 @param month 月
 @return 天数长度
 */
+ (NSInteger)daysWithYear:(NSInteger)year month:(NSInteger)month {
    
    NSString *dateString = [NSString stringWithFormat:@"%zd-%zd-01 00:00:00", year, month];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [formatter dateFromString:dateString];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSRange dayRange = [calendar rangeOfUnit:(NSCalendarUnitDay) inUnit:(NSCalendarUnitMonth) forDate:date];
    
    return dayRange.length;
}

@end
